extends Control

# UI引用
@onready var player_name_label: Label = $MarginContainer/VBoxContainer/TopPanel/VBoxContainer/PlayerName
@onready var realm_label: Label = $MarginContainer/VBoxContainer/TopPanel/VBoxContainer/RealmInfo/RealmLabel
@onready var qi_label: Label = $MarginContainer/VBoxContainer/TopPanel/VBoxContainer/QiInfo/QiLabel
@onready var qi_progress: ProgressBar = $MarginContainer/VBoxContainer/TopPanel/VBoxContainer/QiProgress
@onready var speed_label: Label = $MarginContainer/VBoxContainer/TopPanel/VBoxContainer/SpeedInfo/SpeedLabel
@onready var cultivation_text: Label = $MarginContainer/VBoxContainer/CenterPanel/CultivationText
@onready var breakthrough_button: Button = $MarginContainer/VBoxContainer/BottomPanel/VBoxContainer/BreakthroughButton
@onready var spirit_root_button: Button = $MarginContainer/VBoxContainer/BottomPanel/VBoxContainer/GridContainer/SpiritRootButton
@onready var log_button: Button = $MarginContainer/VBoxContainer/BottomPanel/VBoxContainer/GridContainer/LogButton
@onready var refine_button: Button = $MarginContainer/VBoxContainer/BottomPanel/VBoxContainer/GridContainer/RefineButton

# 弹窗面板
@onready var spirit_root_panel: Panel = $SpiritRootPanel
@onready var log_panel: Panel = $LogPanel
@onready var result_panel: Panel = $ResultPanel

# 游戏管理器
var game_manager: GameManager

# 修炼文字动画计时器
var cultivation_text_timer: float = 0.0
const CULTIVATION_TEXT_INTERVAL = 2.0

func _ready():
	# 获取游戏管理器
	game_manager = get_node("/root/Main/GameManager")
	
	# 连接信号
	game_manager.qi_changed.connect(_on_qi_changed)
	game_manager.realm_changed.connect(_on_realm_changed)
	game_manager.cultivation_speed_changed.connect(_on_cultivation_speed_changed)
	game_manager.breakthrough_result.connect(_on_breakthrough_result)
	
	# 连接按钮信号
	breakthrough_button.pressed.connect(_on_breakthrough_button_pressed)
	spirit_root_button.pressed.connect(_on_spirit_root_button_pressed)
	log_button.pressed.connect(_on_log_button_pressed)
	refine_button.pressed.connect(_on_refine_button_pressed)
	
	# 初始化UI
	player_name_label.text = game_manager.player_name
	_on_realm_changed(game_manager.realm.get_realm_name())
	_on_qi_changed(game_manager.current_qi, game_manager.realm.get_max_qi())
	_on_cultivation_speed_changed(game_manager.get_cultivation_speed())
	
	# 初始化面板为隐藏
	spirit_root_panel.hide()
	log_panel.hide()
	result_panel.hide()

func _process(delta):
	# 更新修炼文字动画
	cultivation_text_timer += delta
	if cultivation_text_timer >= CULTIVATION_TEXT_INTERVAL:
		cultivation_text_timer = 0.0
		cultivation_text.text = game_manager.get_random_cultivation_text()
	
	# 更新突破按钮状态
	breakthrough_button.disabled = game_manager.current_qi < game_manager.realm.get_max_qi()

func _on_qi_changed(current_qi: int, max_qi: int):
	qi_label.text = "✨ " + str(current_qi) + " / " + str(max_qi)
	qi_progress.max_value = max_qi
	qi_progress.value = current_qi
	
	# 根据进度改变进度条颜色 - 使用更鲜艳的渐变色
	var progress_ratio = float(current_qi) / float(max_qi)
	if progress_ratio >= 1.0:
		qi_progress.modulate = Color(0.2, 1.0, 0.4)  # 亮绿色 - 可突破
	elif progress_ratio >= 0.8:
		qi_progress.modulate = Color(1.0, 0.85, 0.2)  # 金黄色 - 接近
	elif progress_ratio >= 0.5:
		qi_progress.modulate = Color(0.4, 0.8, 1.0)  # 天蓝色 - 进行中
	else:
		qi_progress.modulate = Color(0.8, 0.6, 1.0)  # 淡紫色 - 正常

func _on_realm_changed(realm_name: String):
	realm_label.text = "⚡ " + realm_name

func _on_cultivation_speed_changed(speed: int):
	speed_label.text = "💨 %d 灵气/秒" % speed

func _on_breakthrough_button_pressed():
	game_manager.attempt_breakthrough()

func _on_spirit_root_button_pressed():
	# 显示灵根面板
	update_spirit_root_panel()
	spirit_root_panel.show()

func _on_log_button_pressed():
	# 显示日志面板
	update_log_panel()
	log_panel.show()

func _on_refine_button_pressed():
	game_manager.refine_spirit_root()
	# 如果灵根面板打开,更新它
	if spirit_root_panel.visible:
		update_spirit_root_panel()

func _on_breakthrough_result(success: bool, message: String):
	# 显示结果面板
	var result_label = result_panel.get_node("VBoxContainer/ResultLabel")
	var close_button = result_panel.get_node("VBoxContainer/CloseButton")
	
	if success:
		result_label.text = "🎉 " + message
		result_label.modulate = Color(0.2, 1.0, 0.5)  # 亮绿色
	else:
		result_label.text = "💔 " + message
		result_label.modulate = Color(1.0, 0.4, 0.4)  # 柔和红色
	
	if not close_button.pressed.is_connected(_on_result_close_button_pressed):
		close_button.pressed.connect(_on_result_close_button_pressed)
	
	result_panel.show()

func _on_result_close_button_pressed():
	result_panel.hide()

func update_spirit_root_panel():
	var roots_container = spirit_root_panel.get_node("VBoxContainer/ScrollContainer/RootsContainer")
	var close_button = spirit_root_panel.get_node("VBoxContainer/CloseButton")
	
	# 清空现有内容
	for child in roots_container.get_children():
		child.queue_free()
	
	# 添加灵根信息
	for root in game_manager.spirit_root.roots:
		var info = game_manager.spirit_root.get_root_info(root)
		var root_label = Label.new()
		root_label.text = info["emoji"] + " " + info["name"] + "\n" + info["desc"]
		root_label.modulate = info["color"]
		root_label.add_theme_font_size_override("font_size", 18)
		roots_container.add_child(root_label)
		
		# 添加分隔线
		var separator = HSeparator.new()
		roots_container.add_child(separator)
	
	# 添加总加成信息
	var total_label = Label.new()
	total_label.text = "\n✨ 总加成 ✨\n"
	total_label.text += "💎 灵气增长: +%.0f%%\n" % (game_manager.spirit_root.get_total_qi_bonus() * 100)
	total_label.text += "⚡ 修炼速度: +%.0f%%\n" % (game_manager.spirit_root.get_total_speed_bonus() * 100)
	total_label.text += "🎯 突破成功率: +%.0f%%" % (game_manager.spirit_root.get_total_success_rate_bonus() * 100)
	total_label.add_theme_font_size_override("font_size", 20)
	total_label.modulate = Color(1.0, 0.9, 0.3)  # 亮金色
	roots_container.add_child(total_label)
	
	# 连接关闭按钮
	if not close_button.pressed.is_connected(_on_spirit_root_close_button_pressed):
		close_button.pressed.connect(_on_spirit_root_close_button_pressed)

func _on_spirit_root_close_button_pressed():
	spirit_root_panel.hide()

func update_log_panel():
	var logs_container = log_panel.get_node("VBoxContainer/ScrollContainer/LogsContainer")
	var close_button = log_panel.get_node("VBoxContainer/CloseButton")
	
	# 清空现有内容
	for child in logs_container.get_children():
		child.queue_free()
	
	# 添加日志
	var logs = game_manager.get_all_logs()
	for log_text in logs:
		var log_label = Label.new()
		log_label.text = log_text
		log_label.add_theme_font_size_override("font_size", 14)
		log_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
		logs_container.add_child(log_label)
	
	# 连接关闭按钮
	if not close_button.pressed.is_connected(_on_log_close_button_pressed):
		close_button.pressed.connect(_on_log_close_button_pressed)

func _on_log_close_button_pressed():
	log_panel.hide()
